<?php
/**
 * 角色控制器
 * RoleController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * @date: 2014-4-29 下午12:25:22
 * @author: netljc
 * @email:netliujuncheng111@126.com 
 * ==============================================
*/
class RoleController extends AdminController {
    
	public function actionAuth($id) {
		if(isset($_GET['menu_ids'])){
			$menu_ids = !empty($_GET['menu_ids'])?explode(',', trim($_GET['menu_ids'])):array(0);
			
			//弥补js选择不全
			$parent_menu = AdminMenu::model()->findAll('menu_id in('.join(',', $menu_ids).')');
			$aparent = array(0);
			foreach ($parent_menu as $v){
				$aparent = array_merge($aparent,explode(',', $v->pids));
			}
			$menu_ids = array_filter(array_unique($aparent));
			$menu_ids = empty($menu_ids)?'':join(',', $menu_ids);
			$result = AdminRole::model()->updateByPk($id, array('action_list'=>$menu_ids));
			//更新权限缓存
			AdminRole::set_role_auth_cache($id);
			$this->ajaxReturn(0,'操作成功');
		}
		//获取当前角色的菜单
		$role_menu = AdminRole::model()->findByPk($id);
		if($role_menu){
			$cur_menu = explode(',', $role_menu->action_list);
		}else{
			$cur_menu = array(0);
		}
		//获取所以的菜单
		$menu = AdminMenu::getMenu(0,0);
		$return_menu = $this->getChildMenu($menu,$cur_menu);
		die(json_encode($return_menu));
	}
	public function getChildMenu($menu,$cur_menu){
		if(empty($menu)) return false;
		$aReturn='';
		foreach ($menu as $v){
			if(isset($v['child']) && !empty($v['child'])){
				$aReturn[]=array(
								'text'=>$v['menu_name'],
								'id'=>$v['menu_id'],
								//'checked'=>in_array($v['menu_id'], $cur_menu)?true:false,
								'checked'=>false,
								'expanded'=>true,
								'children'=>$this->getChildMenu($v['child'],$cur_menu)
						   );
			}else{
				$aReturn[]=array(
								'text'=>$v['menu_name'],
								'id'=>$v['menu_id'],
								'checked'=>in_array($v['menu_id'], $cur_menu)?true:false,
								'expanded'=>true
						   );
			}
		}
		return $aReturn;
	}
    
	//批量删除角色
	public function actionDeleteall(){
	    $check=1;
	    $ids=explode('|',$_POST['ids']);
	    foreach ((array)$ids as $v){
	        $result=AdminRole::model()->findByPk($v);
	        if($result && $result->delete()){
	            AdminLog::add('删除角色'.$result->role_name);
	        }else{
	            $check=0;
	        }
	    }
	    if($check){
	        $this->ajaxReturn(0,'删除成功');
	    }else{
	        $this->ajaxReturn(1,'删除失败');
	    }
	}
    
	//删除角色
	public function actionDelete($id){
	    $result=AdminRole::model()->findByPk($id);
	    if($result && $result->delete()){
	        AdminLog::add('删除角色'.$result->role_name);
	        $this->ajaxReturn(0,'删除成功');
	    }else{
	        $this->ajaxReturn(1,'删除失败');
	    }
	}
    
	//编辑角色
	public function actionUpdate($id){
	    $model = AdminRole::model()->findByPk($id);
	    if(isset($_POST['ajax']) && $_POST['ajax']==='adminrole-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['AdminRole'])){
	        $model->attributes=$_POST['AdminRole'];
	        if($model->save()){
	        	AdminLog::add('编辑角色'.$model->role_name);
	        	$alert="SendWebMsg('success','角色编辑成功','".$this->createUrl('role/index')."')";
	        }else{
	        	if($model->hasErrors()){
	        		$alert="SendWebMsg('cue','请修正页面错误提示项')";
	        	}else{
	        		$alert="SendWebMsg('error','你的网络不给力，操作失败')";
	        	}
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    $this->render('_form',array('model'=>$model));
	}
    
	//添加角色
	public function actionCreate(){
	    $model = new AdminRole();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='adminrole-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['AdminRole'])){
	        $model->attributes=$_POST['AdminRole'];
	        if($model->save()){
	        	AdminLog::add('添加角色'.$model->role_name);
	        	$alert="SendWebMsg('success','角色添加成功','".$this->createUrl('role/index')."')";
	        }else{
	        	if($model->hasErrors()){
	        		$alert="SendWebMsg('cue','请修正页面错误提示项')";
	        	}else{
	        		$alert="SendWebMsg('error','你的网络不给力，操作失败')";
	        	}
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    $this->render('_form',array('model'=>$model));
	}
    
	// 角色管理
	public function actionIndex() {
	    $criteria = new CDbCriteria();
	    $criteria->order = 'role_id asc';
	    $count = AdminRole::model()->count($criteria);
	    $pages = new CPagination($count);
	    
	    $pages->pageSize = $this->pagesize;
	    $pages->applyLimit($criteria);
	    $model = AdminRole::model()->findAll($criteria);
	    
	    $this->render('index', array('model'=>$model, 'pages'=>$pages));
	}
}